<!-- 用户管理 -->
<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!-- 用户列表 -->
      <el-col :lg="20" :xs="24">
        <div class="search-container">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="请输入" prop="nickname">
              <el-input
                v-model="queryParams.nickname"
                placeholder="用户名/昵称"
                clearable
                style="width: 200px"
                @keyup.enter="handleQuery"
              />
            </el-form-item>
            <el-form-item label="组名" prop="groupName">
              <el-select v-model="queryParams.groupName" placeholder="请选择组名" style="width: 200px">
                <el-option value="0" label="默认" />
                <el-option value="1" label="特别" />
                <el-option value="2" label="悄悄关注" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleQuery">
                <i-ep-search />
                搜索
              </el-button>
              <el-button @click="handleResetQuery">
                <i-ep-refresh />
                重置
              </el-button>
            </el-form-item>
          </el-form>
        </div>

        <el-card shadow="never" class="table-container">
          <template #header>
            <div class="flex-x-between">
            </div>
          </template>

          <el-table
            v-loading="loading"
            :data="pageData"
            @selection-change="handleSelectionChange"
          >

            <el-table-column
              key="id"
              label="用户id"
              align="center"
              prop="followId"
              width="100"
            />
            <el-table-column
              key="username"
              label="用户名"
              align="center"
              prop="username"
              width="120"
            />
            <el-table-column
              label="用户昵称"
              width="120"
              align="center"
              prop="nickname"
            />

            <el-table-column
              label="头像"
              width="100"
              align="center"
              prop="avatar"
            >
              <template #default="scope">
                <el-avatar :src="`http://localhost:8989/image/${scope.row.avatar}`"></el-avatar>
              </template>
            </el-table-column>>

            <el-table-column
              label="分组"
              width="120"
              align="center"
              prop="groupName"
            />
            <el-table-column
              label="手机号码"
              align="center"
              prop="mobile"
              width="120"
            />
            <el-table-column
              label="描述"
              align="center"
              prop="discription"
              width="120"
            />
            <el-table-column
              label="关注时间"
              align="center"
              prop="createTime"
              width="180"
            />
            <el-table-column label="操作" fixed="right" width="220">
              <template #default="scope">
                <el-button
                  v-hasPerm="['sys:user:delete']"
                  type="danger"
                  link
                  size="small"
                  @click="handleDelete(scope.row)"
                >
                  <i-ep-delete />
                  取关
                </el-button>
                <el-button
                  v-hasPerm="['sys:user:delete']"
                  type="primary"
                  link
                  size="small"
                  @click="handleUpdate(scope.row)"
                >
                  <i-ep-delete />
                  修改分组
                </el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-if="total > 0"
            v-model:total="total"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="handleQuery"
          />
        </el-card>
      </el-col>
    </el-row>
  </div>
  <el-drawer v-model="dialog.visible" :title="dialog.title" width="500px">
    <el-form label-width="80px" :model="updateFrom" ref="userFormRef">
      <el-form-item label="分组">
        <el-select v-model="updateFrom.groupName" placeholder="请选择分组">
          <el-option value="1" label="特别"/>
          <el-option value="2" label="悄悄关注"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSubmit">
          <i-ep-check />
          确定
        </el-button>
        <el-button @click="dialog.visible = false">
          <i-ep-close />
          取消
        </el-button>
      </el-form-item>
    </el-form>
  </el-drawer>
</template>

<script setup lang="ts">
defineOptions({
  name: "User",
  inheritAttrs: false,
});

import FollowAPI, { UserFollowQuery, UserFollowVO } from "@/api/follow";

const queryFormRef = ref(ElForm);
const userFormRef = ref(ElForm);


const loading = ref(false);
const removeIds = ref([]);
const total = ref(0);
const pageData = ref<UserFollowVO[]>();
/** 部门下拉选项 */
const deptOptions = ref<UserFollowVO[]>();
/** 角色下拉选项 */
const roleOptions = ref<UserFollowVO[]>();
/** 用户查询参数  */
const queryParams = ref<UserFollowQuery>({
  pageNum: 1,
  pageSize: 10,
  groupName: undefined,
  nickname: undefined,
});

const updateFrom = ref({
  id: -1,
  groupName: "",
})
/**  用户弹窗对象  */
const dialog = reactive({
  visible: false,
  title: "",
});



/** 查询 */
function handleQuery() {
  loading.value = true;
  FollowAPI.getPage(queryParams)
    .then((data) => {
      console.log("handleQuery", data);
      pageData.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置查询 */
function handleResetQuery() {
  queryParams.value.pageNum = 1;
  queryParams.value.nickname = undefined;
  queryParams.value.groupName = undefined;
  handleQuery();
}

/** 行复选框选中记录选中ID集合 */
function handleSelectionChange(selection: any) {
  removeIds.value = selection.map((item: any) => item.id);
}

/** 取关用户 */
const handleDelete = (row: UserFollowVO) => {
  ElMessageBox.confirm(`正在取关用户：${row.username}，确认取关？`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(() => {
    FollowAPI.deleteUserById(row.id).then(() => {
      ElMessage.success("取关成功")
      handleQuery()
    })
  })
}

const handleSubmit = () => {
  updateFrom.value.groupName =="1"? updateFrom.value.groupName = "特别" : updateFrom.value.groupName = "悄悄关注"
  console.log(updateFrom.value)
  ElMessageBox.confirm(`正在修改用户分组，确认修改？`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(() => {
    FollowAPI.updateGroup(updateFrom.value).then(() => {
      ElMessage.success("修改成功")
      dialog.visible = false
      handleQuery()
    })
  })
}

/** 修改分组 */
const handleUpdate = (row: UserFollowVO) => {
      dialog.visible = true
      dialog.title  = `修改分组 ${row.username}`
  if (row.id != null) {
    updateFrom.value.id = row.id
  }
}

onMounted(() => {
  handleQuery();
});
</script>
